import http from '../../apis/api.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    customerAllList: [],
    materialAll: [],
    submitDisable: false,
    formData: {
      shCustomerInfo: '',
      pid: '',/**浆站id */
      name: '',/**物料id */
      cgMaterial: '',/**物料名称 */
      batch: '',
    },
    error: {
      shCustomerInfo: '',
      cgMaterial: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.type = options.type;
    wx.setNavigationBarTitle({
      title: self.data.type === 'add' ? '新增零件' : '修改零件'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.getShCustomerList();
    self.getMaterialAll();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取所有浆站 */
  getShCustomerList() {
    http.shCustomerListAll().then(res => {
      self.data.customerAllList = res.content;
      self.setData({
        customerAllList: self.data.customerAllList
      })
    })
  },
  /** 获取所有备件类型*/
  getMaterialAll(data) {
    http.jqbzAll(data).then(res => {
      self.data.materialAll = res.content;
      console.log(self.data.materialAll);
      self.setData({
        materialAll: self.data.materialAll
      })
    })
  },
  bindPickerChange(e) {
    console.log(e);
    let pickerType = e.currentTarget.dataset.type;
    switch (pickerType) {
      case 'shCustomerInfo':
        break;
    }
    self.data.formData.shCustomerInfo = self.data.customerAllList[e.detail.value].name;
    self.data.formData.pid = self.data.customerAllList[e.detail.value].id;
    self.setData({
      formData: self.data.formData
    })
  },
  selectItem(e) {
    let selectContent = e.detail;
    self.data.formData.cgMaterial = selectContent.name;
    self.data.formData.name = selectContent.id;
    self.setData({
      formData: self.data.formData
    })
  },
  getSearchAll(e) {
    self.getMaterialAll({
      keywords: e.detail
    })
  },
  /**获取input框的值*/
  getValus(e) {
    let value = e.detail.value
    switch (e.currentTarget.dataset.type) {
      case 'batch':
        self.data.formData.batch = value;
        break;
      case 'code':
        self.data.formData.code = value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'shCustomerInfo':
        if (self.data.formData.shCustomerInfo === '') {
          self.data.error.shCustomerInfo = "请选择浆站"
          return false
        }
        self.data.error.shCustomerInfo = '';
        return true;
        break;
      case 'cgMaterial':
        if (self.data.formData.cgMaterial === '') {
          self.data.error.cgMaterial = "请选择备件"
          return false
        }
        self.data.error.cgMaterial = '';
        return true;
        break;
    }
  },
  /**点击提交*/
  submitInfo() {
    self.setData({
      submitDisable: true
    })
    if (self.checked('shCustomerInfo') && self.checked('cgMaterial')) {
      let postData = {
        pid: self.data.formData.pid,
        name: self.data.formData.name,
        batch: self.data.formData.batch,
        code: self.data.formData.code
      };
      http.insertParts(postData).then(res => {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000,
          success() {
            wx.navigateBack();
          },
          complete() {
            self.setData({
              submitDisable: false
            })
          }
        })
      }, error => {
        self.setData({
          submitDisable: false
        })
      })
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  },
})